//打家劫舍II
class Solution {
public:
    int rob(vector<int>& nums) {

        int n = nums.size();
        if (n == 1)
            return nums[0];
        if (n == 2)
            return max(nums[0], nums[1]);
        if (n >= 3)
            return max(nums[0] + rob1(nums, 2, n - 3), rob1(nums, 1, n - 1));
        else
            return rob1(nums, 1, n - 1);
    }
    int rob1(vector<int>& nums, int l, int n) {
        if (!n)
            return 0;
        vector<int> f(n);
        auto g = f;
        f[0] = nums[l];
        g[0] = 0;
        for (int i = 1; i < n; i++) {
            f[i] = g[i - 1] + nums[i + l];
            g[i] = max(f[i - 1], g[i - 1]);
        }
        return max(f[n - 1], g[n - 1]);
    }
};

//删除并获得点数
class Solution {
    int hash[10001] = {0};

public:
    int deleteAndEarn(vector<int>& nums) {
        int _min = 0;
        for (auto& num : nums) {
            hash[num]++;
            _min = max(num, _min);
        }
        vector<int> f(_min + 1);
        auto g = f;
        // cout << f.size();
        for (int i = 1; i <= _min; i++) {
            f[i] = g[i - 1] + hash[i] * i;
            //cout << f[i] << endl;
            g[i] = max(g[i - 1], f[i - 1]);
        }
        return max(f[_min], g[_min]);
    }
};